/**
 * 
 */
package vn.edu.hungvuongaptech.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import vn.edu.hungvuongaptech.common.Constant;
import vn.edu.hungvuongaptech.model.ProjectModel;
import vn.edu.hungvuongaptech.util.DataUtil;

/**
 * @author ThanhHQ
 *
 */
public class ProjectDAO {

	/**
	 * Get All Projects.
	 * @return ArrayList<ProjectModel>
	 */
	public static ArrayList<ProjectModel> getAllProject() {
		ArrayList<ProjectModel> projectModelList = new ArrayList<ProjectModel>();
		try {
			String sql = Constant.SQL_RES.getString("timesheet.sql.getAllProjects");
			ResultSet rs = DataUtil.executeQuery(sql);
			ProjectModel projectModel = null;
			while (rs.next()) {
				projectModel = new ProjectModel();
				projectModel.setProjectId(rs.getLong("ProjectId"));
				projectModel.setProjectName(rs.getString("ProjectName"));
				projectModel.setStartDate(rs.getString("StartDate"));
				projectModel.setFinishDate(rs.getString("FinishDate"));
				projectModel.setProjectDescription(rs.getString("ProjectDescription"));
				projectModelList.add(projectModel);
			}
			return projectModelList;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	public static ArrayList<ProjectModel> getProjectByUser(long userID){
		ArrayList<ProjectModel> projectModelList = new ArrayList<ProjectModel>();
		ProjectModel projectModel= new ProjectModel();
		try {
			PreparedStatement preparedStatement = DataUtil
					.getConnection()
					.prepareStatement(
							Constant.SQL_RES
									.getString("timesheet.sql.getProjectByUser"));
			preparedStatement.setLong(1, userID);
			ResultSet rs = preparedStatement.executeQuery();
			while (rs.next()) {
				projectModel.setProjectId(rs.getLong("ProjectId"));
				projectModel.setProjectName(rs.getString("ProjectName"));
				projectModelList.add(projectModel);
			}
			return projectModelList;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	
	}
	
	public static ProjectModel getProjectByID(long productID){
		ProjectModel projectModel = new ProjectModel();		
		try {
			PreparedStatement preparedStatement = DataUtil
					.getConnection()
					.prepareStatement(
							Constant.SQL_RES
									.getString("timesheet.sql.getProjectByProjectID"));
			preparedStatement.setLong(1, productID);
			ResultSet rs = preparedStatement.executeQuery();
			while (rs.next()) {
				projectModel.setProjectId(rs.getLong("ProjectId"));
				projectModel.setProjectName(rs.getString("ProjectName"));
				projectModel.setStartDate(rs.getString("StartDate"));
				projectModel.setFinishDate(rs.getString("FinishDate"));
				projectModel.setProjectDescription(rs.getString("ProjectDescription"));
			}
			return projectModel;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	
	}
}
